import axios from 'axios'
import type { AxiosInstance } from 'axios'
import type { HYRequestInterceptors, HYRequestConfig } from './type'

import { ElLoading } from 'element-plus'
import type { LoadingInstance } from 'element-plus/lib/components/loading/src/loading'

const DEFAULT_LOADING = true

class Request {
  instacne: AxiosInstance
  interceptors?: HYRequestInterceptors
  loading?: LoadingInstance
  showLoading: boolean

  constructor(config: HYRequestConfig) {
    //创建axios实例
    this.instacne = axios.create(config)
    this.interceptors = config.interceptors

    //保存基本信息
    this.showLoading = config.showLoading ?? DEFAULT_LOADING

    //使用拦截器
    //1. 从config中取出的拦截器是对应的实例订单拦截器
    this.instacne.interceptors.request.use(
      this.interceptors?.requestInterceptors,
      this.interceptors?.requestInterceptorsCatch
    )
    this.instacne.interceptors.response.use(
      this.interceptors?.responseInterceptors,
      this.interceptors?.responseInterceptorsCatch
    )

    //2. 全局的请求拦截器和响应拦截器
    this.instacne.interceptors.request.use(
      (config) => {
        if (this.showLoading) {
          this.loading = ElLoading.service({
            lock: true,
            text: '正在请求数据...',
            //spinner: 'el-loading',
            background: 'rgba(0, 0, 0, .5)'
          })
        }

        //console.log('全局的请求成功拦截')
        return config
      },
      (err) => {
        //console.log('全局的请求失败拦截')
        return err
      }
    )

    this.instacne.interceptors.response.use(
      (res) => {
        //将loading移除
        this.loading?.close()

        //console.log('全局的响应成功拦截')
        return res.data
      },
      (err) => {
        //将loading移除
        this.loading?.close()

        //console.log('全局的响应失败拦截')
        //响应失败的处理
        //switch(err.response.status) {
        //  case 404:
        //  case 503
        //  ...
        //}
        return err
      }
    )
  }

  request<T = any>(config: HYRequestConfig<T>): Promise<T> {
    return new Promise((resolve, reject) => {
      //判断当前方法下是否有请求拦截器有的话赋值给config
      if (config.interceptors?.requestInterceptors) {
        config = config.interceptors.requestInterceptors(config)
      }

      //判断请求方法是否有showLoading,没有不显示
      if (config.showLoading === false) {
        this.showLoading = config.showLoading
      }

      this.instacne
        .request<any, T>(config)
        .then((res) => {
          //1. 单个请求对数据的处理
          if (config.interceptors?.responseInterceptors) {
            res = config.interceptors.responseInterceptors(res)
          }

          //2. 将showLoading设置为true,这样不会影响下一个请求
          this.showLoading = DEFAULT_LOADING

          //3. 将结果resolve返回出去
          resolve(res)
        })
        .catch((err) => {
          this.showLoading = DEFAULT_LOADING
          reject(err)
          return err
        })
    })
  }

  get<T = any>(config: HYRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'GET' })
  }

  post<T = any>(config: HYRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'POST' })
  }

  delete<T = any>(config: HYRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'DELETE' })
  }

  patch<T = any>(config: HYRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: 'PATCH' })
  }
}

export default Request
